package net.dgg.zz.api.production.controller;

import io.swagger.annotations.Api;
import net.dgg.common.tools.exception.ExceptionUtils;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.zz.api.production.service.ApiUnderOrderService;
import net.dgg.zz.production.excption.ProductOrderExcption;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>@Title 生产部分对外接口controller</p>
 * <p>@Description 办理中相关</p>
 * <p>@Version 1.0.0 版本号</p>
 * <p>@author maoquanyi</p>
 * <p>@date 2018年08月13日</p>
 * <p>Copyright © dgg group.All Rights Reserved. 版权信息</p>
 */
@Controller
@RequestMapping("/api/zzsc/under_order")
@Api(description = "生产给IBOSS提供的接口")
public class ApiUnderOrderController extends DggBaseController {
    @Autowired
    private ApiUnderOrderService apiUnderOrderService;
    /**
     * iboos订单插入接口    订单中心使用
     * todo  iboss下单第一步   确认跳转业务底单地址
     *
     * @param order     产品
     * @param products  商品
     * @param returnUrl 返回地址
     * @param notifyUrl 异步通知地址
     * @return
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @ResponseBody
    @RequestMapping("/v1/zz_under_order.do")
    public DggRestResponse placeAnOrderV2(@RequestParam String order, @RequestParam String products, @RequestParam String returnUrl, @RequestParam String notifyUrl) {
        try {
            return this.getSuccessResponse(apiUnderOrderService.returnUrl(order,products,returnUrl,notifyUrl,true));
        } catch (DggBaseException e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, "/api/zzsc/under_order/v1/zz_under_order.do", order, products, returnUrl, notifyUrl);
            return this.getFailResponse("查询底单失败！");
        }
    }
    /**
     * 底单中心跳转到底单页面
     *
     * @param model
     * @param orderId           销售订单id
     * @param businessMark      请求这个接口 页面标签的url地址
     * @param directMainOrderId 主订单id
     * @return
     */
    @RequestMapping(value = "/edit.html")
    public String editIndex(Model model, @RequestParam Long orderId, Boolean isOrderCenter, String businessMark, Long directMainOrderId) {
        try {
            // 跳转用户选择的主订单
            // 二期除版权业务字段
            model.addAttribute("directMainOrderId", null == directMainOrderId ? "" : directMainOrderId);
            model.addAttribute("isOrderCenter", null == isOrderCenter ? false : isOrderCenter);
            //如果是后台请求编辑，businessmark 不为空  fixed 表示是iboss请求编辑
            businessMark = null == businessMark ? "fixed" : businessMark;
            return apiUnderOrderService.underOrderIndex(true, orderId, businessMark, model);
        } catch (Exception e) {
            ExceptionUtils.error(e, "/api/zzsc/under_order/edit.html", orderId, businessMark);
        }
        return "error/500";
    }

    /**
     * <p>@Description 保存底单信息</p>
     * <p>@author sdx</p>
     * <p>@Time 2021年2月23日14:30:37 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/save_under_order")
    @ResponseBody
    @NLog()
    public DggRestResponse saveUnderOrder(@RequestParam Long orderId,@RequestParam String params) {
        try {
            apiUnderOrderService.saveUnderOrder(orderId, params);
            return this.getSuccessResponse("验证通过!");
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("系统异常,请联系管理员!");
        }
    }
}
